<template>
  <div class="detail-header">
    <router-link to="/" class="header-abs iconfont back-icon-back" v-show="showAbs" :style="opacityFixedStyle"
      >&#xe624;</router-link
    >
    <div class="header-fixed" v-show="!showAbs" :style="opacityFixedStyle">
      <router-link to="/">
        <div class="iconfont header-fixed-back">&#xe624;</div>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data() {
    return {
      showAbs: true,
      opacityFixedStyle: 0,
    }
  },

  methods: {
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset

      if (scrollTop > 40) {
        // 固定标题栏的渐变
        let opacity = scrollTop / 130
        opacity = opacity > 1 ? 1 : opacity
        this.opacityFixedStyle = {
          opacity,
        }
        this.showAbs = false
      } else {
        // 返回键的渐变
        this.showAbs = true
        // let opacity = scrollTop / 40
        // opacity = opacity > 1 ? 1 : opacity
        // this.opacityAbsStyle = {
        //   opacity: 1 - opacity
        // }
      }
    },
  },

  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  destroyed() {
    window.removeEventListener('scroll', this.handleScroll)
  },
}
</script>

<style lang="scss" scoped>
.detail-header {
  position: relative;
  z-index: 99;
}
.header-abs {
  position: absolute;
  margin-left: 0.2rem;
  top: 0.2rem;
  text-align: center;
  width: 0.6rem;
  height: 0.6rem;
  line-height: 0.6rem;
  border-radius: 0.3rem;
  background: rgba(0, 0, 0, 0.5);
}
.header-abs .back-icon-back {
  color: #fff;
  font-size: 0.3rem;
  margin-left: -0.06rem;
}
.header-fixed {
  z-index: 2;
  position: fixed;
  top: 0;
  width: 100%;
  max-width: 13rem;
  overflow: hidden;
  height: 0.86rem;
  line-height: 0.86rem;
  background: $bg;
  text-align: center;
  color: #fff;
  font-size: 0.4rem;
}
.header-fixed .header-fixed-back {
  position: absolute;
  top: 0;
  left: 0 0.5rem;
  width: 0.64rem;
  text-align: center;
  font-size: 0.4rem;
  color: #fff;
}
</style>
